/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fragment

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.gitee.wsl.common.ui.spannable.bubbleBackground
import com.gitee.wsl.common.ui.spannable.header
import com.gitee.wsl.common.ui.spannable.inlineCode
import com.gitee.wsl.android.ext.alignmentCenter
import com.gitee.wsl.android.ext.alignmentOpposite
import com.gitee.wsl.android.ext.backgroundColor
import com.gitee.wsl.android.ext.bold
import com.gitee.wsl.android.ext.br
import com.gitee.wsl.android.ext.color
import com.gitee.wsl.android.ext.image
import com.gitee.wsl.android.ext.italic
import com.gitee.wsl.android.ext.lineBackground
import com.gitee.wsl.android.ext.newLine
import com.gitee.wsl.android.ext.plus
import com.gitee.wsl.android.ext.quote
import com.gitee.wsl.android.ext.sampleSpannable
import com.gitee.wsl.android.ext.scaleX
import com.gitee.wsl.android.ext.spannable
import com.gitee.wsl.android.ext.subscript
import com.gitee.wsl.android.ext.superscript
import com.gitee.wsl.android.ext.underline
import com.gitee.wsl.android.ext.url
import com.google.android.catalog.app.fragment.R
import com.google.android.catalog.framework.annotations.Sample

@Sample(name = "Spannable Fragment Sample", description = "A sample target that uses a fragment")
class FragmentSample : Fragment(R.layout.fragment_sample){

    val defString=""" 
     粗体 `**` `__`
     斜体 `*` `_`
     粗体和斜体嵌套
     scaleX
     图片 `![]()`
     链接 `[]()`
     行内代码
     删除线 `~~`
     这是上标
     这是下标
     ---
     标题1 #
     标题2 ##
     标题3 ###
     标题4 ####
     标题5 #####
     标题6 ######
     ***
     前景色
     背景色
     整行背景色
     居左
     居中
     居右
     区域引用 `> `
     嵌套区域引用 `> > `
     注脚 `[^]`
     代码区块
     反斜杠 `\`
     Todo `- [ ] ` / `- [x]`
     表格 `| 表格 | 表格 |`
     代码高亮
     有序列表 `1. `
     嵌套有序列表
     无序列表 `* ` /  `+ ` / `- `
     嵌套无序列表
    """


    private val sampleSpannable= sampleSpannable {
        text(" a text ").br()
        bold {
            text(" body text ").br()
            italic { text(" italic ") }.br()
            bold {
                italic {
                    text("bold and italic")
                }
            }
            newLine()
        }
    }

    private val strSpannable="text is a spannable".spannable {
         color(Color.RED,"is")
         backgroundColor(Color.BLUE,"spannable")
        at("text"){
            bubbleBackground()
        }
    } +"\n"+
            "test is send".spannable {
            color(Color.BLUE,"is")
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    private val defSpannable = defString.spannable {
        at("粗体 `**` `__`"){
            bold()
        }
        at("斜体 `*` `_`"){
            italic()
        }
        at("粗体和斜体嵌套"){
            bold()
            italic()
        }
        at("scaleX"){
            scaleX(1.5f)
        }

        at("图片 `![]()`"){
           image(R.drawable.ic_edit)
        }
        at("链接 `[]()`"){
           url("http://tt")
        }
        at("行内代码"){
           inlineCode()
        }
        at("删除线 `~~`"){
           underline()
        }
        at("标题1"){
           header(1)
        }
        at("标题2"){
            header(2)
        }
        at("标题3"){
            header(3)
        }
        at("标题4"){
            header(4)
        }
        at("标题5"){
            header(5)
        }
        at("标题6"){
            header(6)
        }
        at("区域引用 `> `"){
            quote()
        }
        at("上标"){
           superscript()
        }
        at("下标"){
          subscript()
        }
        at("前景色"){
            color(Color.GREEN)
        }
        at("背景色"){
           backgroundColor(Color.YELLOW)
        }
        at("整行背景色"){
            lineBackground(Color.RED)
        }
        at("居中"){
            alignmentCenter()
        }
        at("居右"){
            alignmentOpposite()
        }
        /*at("---"){

        }
        at("***"){

        }*/

    }


    lateinit var  sampleText:TextView;
    lateinit var  sampleText1:TextView;
    lateinit var  sampleText2:TextView;

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val main=super.onCreateView(inflater, container, savedInstanceState)
        sampleText = main!!.findViewById(R.id.sample_text)
        sampleText.setText(strSpannable)
        sampleText1 = main.findViewById(R.id.sample_text1)
        sampleText1.setText(sampleSpannable)
        sampleText2 = main.findViewById(R.id.sample_text2)
        sampleText2.setText(defSpannable)
        return main

    }
}
